package com.dianban.service.impl;


import com.dianban.dao.FacePayCommissionVerifyLogDao;
import com.dianban.dao.UserDao;
import com.dianban.entity.FacePayCommissionLog;
import com.dianban.entity.FacePayCommissionVerifyLog;
import com.dianban.entity.FacePayConfig;
import com.dianban.service.FacePayCommissionLogService;
import com.dianban.service.FacePayCommissionVerifyLogService;
import com.dianban.service.FacePayConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户业务实现层
 * <p>
 * Created by bysocket on 07/02/2017.
 */
@Service
@Transactional(readOnly = true)
public class FacePayCommissionVerifyLogServiceImpl implements FacePayCommissionVerifyLogService {

    @Autowired
    private FacePayCommissionVerifyLogDao facePayCommissionVerifyLogDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private FacePayCommissionLogService facePayCommissionLogService;
    @Autowired
    private FacePayConfigService facePayConfigService;

    @Override
    @Transactional(readOnly = false)
    public int insert(FacePayCommissionVerifyLog facePayCommissionVerifyLog) {
        facePayCommissionVerifyLog.setStatus(10);
        facePayCommissionVerifyLog.setCreatetime(System.currentTimeMillis() / 1000);
        return facePayCommissionVerifyLogDao.insert(facePayCommissionVerifyLog);
    }

    @Override
    public FacePayCommissionVerifyLog findById(Long id) {
        return facePayCommissionVerifyLogDao.findById(id);
    }

    @Override
    public List<FacePayCommissionVerifyLog> findList(Integer status, Long userId, Long offlineOrderId, Integer effectiveMinute) {
        return facePayCommissionVerifyLogDao.findList(status, userId, offlineOrderId, effectiveMinute);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateState(Long id, Integer status) {
        return facePayCommissionVerifyLogDao.updateState(id, status, System.currentTimeMillis() / 1000);
    }

    @Override
    @Transactional(readOnly = false)
    public int verify(FacePayCommissionVerifyLog facePayCommissionVerifyLog) {
        int count = 0;
        Long offlineOrderId = facePayCommissionVerifyLog.getOfflineOrderId();
        // 有效期改成配置
        List<FacePayConfig> facePayConfigList = facePayConfigService.find(120, 1, null);
        Integer effectiveMinute = 30;
        if (facePayConfigList != null && !facePayConfigList.isEmpty()) {
            effectiveMinute = Integer.parseInt(facePayConfigList.get(0).getValue());
        }
        List<FacePayCommissionVerifyLog> facePayCommissionVerifyLogList = findList(10, null, offlineOrderId, effectiveMinute);
        for (FacePayCommissionVerifyLog facePayCommissionVerifyLog1 : facePayCommissionVerifyLogList) {
            BigDecimal facePayCommission = facePayCommissionVerifyLog1.getMoney();
            Long userId = facePayCommissionVerifyLog1.getUserId();
            Long id = facePayCommissionVerifyLog1.getId();
            count = userDao.updateFacePayCommission(facePayCommission, userId);
            if (count > 0) {
                FacePayCommissionLog facePayCommissionLog = new FacePayCommissionLog();
                BeanUtils.copyProperties(facePayCommissionVerifyLog1, facePayCommissionLog, "id");
                facePayCommissionLogService.insert(facePayCommissionLog);
                updateState(id, 20);
            }
        }
        return count;
    }
}
